﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

using Microsoft.ApplicationBlocks.Data;

using YouHP.IDAL;
using YouHP.Model;

namespace YouHP.SqlServerDAL
{
    public class TreatmentDAL : ITreatment
    {
        #region 治疗基本操作
        public TreatmentInfo GetTreatmentInfo(int id)
        {
            IDataReader reader = SqlHelper.ExecuteReader(Common.ConnStr,
                Common.GetFullName("GetTreatment"),
                id) as IDataReader;
            return CBO.FillObject<TreatmentInfo>(reader);
        }

        public bool AddTreatment(TreatmentInfo info)
        {
            int cnt = SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.TreatmentSP_AddTreatmentInfo,
                info.Name, info.PinYin, info.Type, info.IsQuantified, info.MeasurementUnits, info.LimitValues, info.ChoicesDescription, info.Intro,
                info.Desp, info.UpdateTime,
                info.PatientId, info.PatientCount,
                info.GeneralName,
                info.Merchant,
                info.Frequency
                );
            return cnt > 0;
        }

        public bool UpdateTreatment(TreatmentInfo info)
        {
            int cnt = SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.TreatmentSP_UpdateTreatment,
                info.Id, info.Name, info.PinYin, info.Type, info.IsQuantified, info.MeasurementUnits, info.LimitValues, info.ChoicesDescription,
                info.Intro, info.Desp, info.UpdateTime,
                info.PatientId,
                info.GeneralName,
                info.Merchant,
                info.PatientName,
                info.Frequency
                );
            return cnt > 0;
        }

        public bool DeleteTreatment(int[] dszIds)
        {
            int cnt = dszIds.Length;
            if (cnt <= 0) return false;
            StringBuilder sb = new StringBuilder();
            sb.Append("[id] in (");
            sb.Append(dszIds[0]);

            for (int i = 1; i < cnt; i++)
            {
                sb.Append(", ");
                sb.Append(dszIds[i]);
            }
            sb.Append(")");

            int cnt2 = SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.TreatmentSP_DelTreatmentInfo,
                sb.ToString());
            return (cnt == cnt2);
        }
        #endregion

        #region 治疗检索
        private List<TreatmentInfo> GetTreatmentList(int pageSize, int pageIndex, string condStr, string orderStr, out int count)
        {
            DataSet dset = SqlHelper.ExecuteDataset(Common.ConnStr,
                 Common.TreatmentSP_GetTreatmentList,
                 pageSize, pageIndex, condStr, orderStr);

            IDataReader dr = dset.Tables[0].CreateDataReader();
            if (dr != null && dr.Read())
            {
                count = dr.GetInt32(0);
                dr.Close();
            }
            else
            {
                count = 0;
            }

            dr = dset.Tables[1].CreateDataReader();
            return CBO.FillCollection<TreatmentInfo>(dr);
        }
        public List<TreatmentInfo> GetTreatmentListOrderByDiseaseId(int iDiseaseId,int pageSize, int pageIndex,out int count)
        {
            DataSet dset = SqlHelper.ExecuteDataset(Common.ConnStr,
                 Common.TreatmentSP_GetTreatmentListOrderbyDiseaseId,
                 iDiseaseId,pageSize, pageIndex);

            IDataReader dr = dset.Tables[0].CreateDataReader();
            if (dr != null && dr.Read())
            {
                count = dr.GetInt32(0);
                dr.Close();
            }
            else
            {
                count = 0;
            }

            dr = dset.Tables[1].CreateDataReader();
            return CBO.FillCollection<TreatmentInfo>(dr);
        }

        private List<TreatmentInfo> GetTreatmentListByDisease(int pageSize, int pageIndex, string condStr, string orderStr, out int count)
        {
            DataSet dset = SqlHelper.ExecuteDataset(Common.ConnStr,
                 Common.TreatmentSP_GetTreatmentListByDisease,
                 pageSize, pageIndex, condStr, orderStr);

            IDataReader dr = dset.Tables[0].CreateDataReader();
            if (dr != null && dr.Read())
            {
                count = dr.GetInt32(0);
                dr.Close();
            }
            else
            {
                count = 0;
            }

            dr = dset.Tables[1].CreateDataReader();
            return CBO.FillCollection<TreatmentInfo>(dr);
        }

        private List<TreatmentInfo> GetTreatmentListWithPatientInfo(int pageSize, int pageIndex, string condStr, string orderStr, out int count)
        {
            DataSet dset = SqlHelper.ExecuteDataset(Common.ConnStr,
                 Common.TreatmentSP_GetTreatmentListWithPatientInfo,
                 pageSize, pageIndex, condStr, orderStr);

            IDataReader dr = dset.Tables[0].CreateDataReader();
            if (dr != null && dr.Read())
            {
                count = dr.GetInt32(0);
                dr.Close();
            }
            else
            {
                count = 0;
            }

            dr = dset.Tables[1].CreateDataReader();
            return CBO.FillCollection<TreatmentInfo>(dr);
        }

        private List<TreatmentInfo> GetTreatmentListByDiseaseWithPatientInfo(int pageSize, int pageIndex, string condStr, string orderStr, out int count)
        {
            DataSet dset = SqlHelper.ExecuteDataset(Common.ConnStr,
                 Common.TreatmentSP_GetTreatmentListByDiseaseWithPatientInfo,
                 pageSize, pageIndex, condStr, orderStr);

            IDataReader dr = dset.Tables[0].CreateDataReader();
            if (dr != null && dr.Read())
            {
                count = dr.GetInt32(0);
                dr.Close();
            }
            else
            {
                count = 0;
            }

            dr = dset.Tables[1].CreateDataReader();
            return CBO.FillCollection<TreatmentInfo>(dr);
        }

        /*按条件检索治疗列表*/
        public List<TreatmentInfo> GetTreatmentList(int pageSize, int pageIndex, string condStr, out int count)
        {
            return GetTreatmentList(pageSize, pageIndex, condStr, " [Treatment].[PatientCount] desc ", out count);
        }

        public List<TreatmentInfo> GetTreatmentListWithOrder(int pageSize, int pageIndex, string condStr,string sOrder, out int count)
        {
            return GetTreatmentList(pageSize, pageIndex, condStr, sOrder, out count);
        }

        /*按疾病条件检索治疗列表*/
        public List<TreatmentInfo> GetTreatmentListByDisease(int pageSize, int pageIndex, string condStr, out int count)
        {
            return GetTreatmentListByDisease(pageSize, pageIndex, condStr, " [Treatment].[PatientCount] desc ", out count);
        }

        /*按条件检索治疗列表（包括典型病人信息）*/
        public List<TreatmentInfo> GetTreatmentListWithPatientInfo(int pageSize, int pageIndex, string condStr, out int count)
        {
            return GetTreatmentListWithPatientInfo(pageSize, pageIndex, condStr, " [Treatment].[PatientCount] desc ", out count);
        }

        /*按疾病条件检索治疗列表（包括典型病人信息）*/
        public List<TreatmentInfo> GetTreatmentListByDiseaseWithPatientInfo(int pageSize, int pageIndex, string condStr, out int count)
        {
            return GetTreatmentListByDiseaseWithPatientInfo(pageSize, pageIndex, condStr, " [Treatment].[PatientCount] desc ", out count);
        }

        /*按照治疗名称来检索治疗*/
        public List<TreatmentInfo> GetTreatmentByName(int pageSize, int pageIndex, string keyword, out int count)
        {
            string condstr = string.Format(@" [treatmentname] like '%{0}%' ", keyword);
            return GetTreatmentList(pageSize, pageIndex, condstr, " [Treatment].[PatientCount] desc ", out count);
        }
        
        /*按照治疗名称拼音字母来检索治疗*/
        public List<TreatmentInfo> GetTreatmentByPinYin(int pageSize, int pageIndex, string pinYin, out int count)
        {
            string condstr = string.Format(@" [firstsymbolofname] like '%{0}%' ", pinYin.ToUpper());
            return GetTreatmentList(pageSize, pageIndex, condstr, " [Treatment].[PatientCount] desc ", out count);
        }

        public List<TreatmentInfo> GetTreatmentByType(int pageSize, int pageIndex, int treatmentTypeId, out int count)
        {
            string condstr = string.Format(@" [treatmenttypeId]={0} ", treatmentTypeId);
            return GetTreatmentList(pageSize, pageIndex, condstr, " [Treatment].[PatientCount] desc ", out count);
        }
        
        #endregion

        #region 治疗统计
        public List<TreatmentStatDosage> GetTreatmentStatDosage(int tid)
        {
            IDataReader reader = SqlHelper.ExecuteReader(Common.ConnStr,
                Common.TreatmentSP_GetTreatmentStatDosage,
                tid) as IDataReader;
            return CBO.FillCollection<TreatmentStatDosage>(reader);
        }

        public List<TreatmentStatTaketime> GetTreatmentStatTaketime(int tid)
        {
            IDataReader reader = SqlHelper.ExecuteReader(Common.ConnStr,
                Common.TreatmentSP_GetTreatmentStatTaketime,
                tid) as IDataReader;
            return CBO.FillCollection<TreatmentStatTaketime>(reader);
        }
        #endregion

        #region 治疗类型
        public bool AddTreatmentType(TreatmentTypeInfo info)
        {
            int cnt = SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.TreatmentSP_AddTreatmentType,
                info.Name, info.ParentId);
            return (cnt > 0);
        }

        public bool DelTreatmentType(int[] itemIds)
        {
            int cnt = itemIds.Length;
            if (cnt <= 0)
                return false;

            StringBuilder sb = new StringBuilder();
            sb.Append("[id] in (");
            sb.Append(itemIds[0]);

            for (int i = 1; i < cnt; i++)
            {
                sb.Append(", ");
                sb.Append(itemIds[i]);
            }
            sb.Append(")");

            int cnt2 = SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.TreatmentSP_DelTreatmentType,
                sb.ToString());
            return cnt2 >= cnt;
        }

        public bool UpdateTreatmentType(TreatmentTypeInfo info)
        {
            int cnt = SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.TreatmentSP_UpdateTreatmentType,
                info.Id, info.Name, info.ParentId);
            return (cnt > 0);
        }

        public TreatmentTypeInfo GetTreatmentTypeInfo(int typeId)
        {
            IDataReader reader = SqlHelper.ExecuteReader(Common.ConnStr,
                Common.TreatmentSP_GetTreatmentType,
                typeId);
            return CBO.FillObject<TreatmentTypeInfo>(reader);
        }

        public List<TreatmentTypeInfo> GetTreatmentTypeList(int parentId)
        {
            string condstr = string.Format(" [parentid]={0} ", parentId);
            return GetTreatmentTypeList(condstr);
        }

        public List<TreatmentTypeInfo> GetTreatmentTypeList()
        {
            return GetTreatmentTypeList("");
        }

        private List<TreatmentTypeInfo> GetTreatmentTypeList(string condstr)
        {
            IDataReader reader = SqlHelper.ExecuteReader(Common.ConnStr,
                Common.TreatmentSP_GetTreatmentTypeList,
                condstr);
            return CBO.FillCollection<TreatmentTypeInfo>(reader);
        }
        #endregion
    }
}
